package com.yzz.composeweatherproject.activity

import androidx.activity.viewModels
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.yzz.composeweatherproject.R
import com.yzz.composeweatherproject.activity.ui.theme.ComposeWeatherProjectTheme
import com.yzz.composeweatherproject.entity.Forecast24Bean
import com.yzz.composeweatherproject.entity.ForecastAfterDay
import com.yzz.composeweatherproject.entity.LocationBean
import com.yzz.composeweatherproject.viewmodel.DataViewModel

class HomeActivity : BaseActivity() {
    private val mViewModel by viewModels<DataViewModel>()

    @Composable
    override fun initView() {
        // A surface container using the 'background' color from the theme
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colors.background
        ) {
            val collectAsState = mViewModel.mLocationWeatherFlow.collectAsState()
            val bean = collectAsState.value ?: return@Surface
            Greeting2(bean)
        }
        mViewModel.getLocationData()
    }
}


@Composable
fun Greeting2(bean: LocationBean) {
    LazyColumn {
        item {
            Column {
                Spacer(modifier = Modifier.size(100.dp))
                Text(text = bean.name, modifier = Modifier.align(Alignment.CenterHorizontally))
                Text(
                    text = bean.temperature,
                    modifier = Modifier.align(Alignment.CenterHorizontally),
                    fontSize = 60.sp
                )
                AsyImage(url = bean.imgUrl, Modifier.align(Alignment.CenterHorizontally))
                Text(
                    text = bean.airQuality,
                    modifier = Modifier.align(Alignment.CenterHorizontally)
                )

                Text(
                    text = bean.weatherState,
                    modifier = Modifier.align(Alignment.CenterHorizontally)
                )
                Text(text = bean.time, modifier = Modifier.align(Alignment.CenterHorizontally))
                Spacer(modifier = Modifier.size(100.dp))
            }
        }
        item {
            Column {
                Text(
                    text = bean.fo24title,
                    Modifier.padding(top = 10.dp, bottom = 10.dp),
                    style = MaterialTheme.typography.h6
                )
                //24小时天气预报
                LazyRow(content = {
                    items(bean.forecast24Bean) { b ->
                        Column(modifier = Modifier.padding(start = 5.dp, end = 5.dp)) {
                            Text(
                                text = b.time,
                                modifier = Modifier.align(Alignment.CenterHorizontally)
                            )
                            AsyImage(b.weatherUrl)
                            Text(
                                text = b.temperature,
                                modifier = Modifier.align(Alignment.CenterHorizontally)
                            )
                        }
                    }
                })
                Text(text = "未来天气", style = MaterialTheme.typography.h6)
                LazyRow {
                    items(bean.forecastAfterDay) { b ->
                        Column {
                            Text(
                                text = b.time,
                                modifier = Modifier.align(Alignment.CenterHorizontally)
                            )
                            Text(
                                text = b.date,
                                modifier = Modifier.align(Alignment.CenterHorizontally)
                            )
                            AsyImage(b.imgUrl)
                            Text(
                                text = b.weatherState,
                                modifier = Modifier.align(Alignment.CenterHorizontally)
                            )
                            Text(
                                text = b.weatherTemperature,
                                modifier = Modifier.align(Alignment.CenterHorizontally)
                            )
                            Text(
                                text = b.airQuality,
                                modifier = Modifier.align(Alignment.CenterHorizontally)
                            )
                        }
                    }
                }
            }
        }
    }


}

@Composable
private fun AsyImage(url: String?, modifier: Modifier = Modifier.clip(CircleShape)) {
    AsyncImage(
        model = ImageRequest.Builder(LocalContext.current)
            .data(url)
            .crossfade(true)
            .build(),
        placeholder = if (mPreviewBoolean) {
            painterResource(R.drawable.ic_launcher_background)
        } else {
            null
        },
        contentDescription = stringResource(R.string.asyn_image),
        contentScale = ContentScale.Crop,
        modifier = modifier
    )
}

var mPreviewBoolean = false

@Preview(showBackground = true)
@Composable
fun DefaultPreview2() {
    mPreviewBoolean = true
    val imgUrl = "https://www.tianqi.com/static/wap2018/ico1/b1.png"
    val list24 = ArrayList<Forecast24Bean>()
    list24.add(Forecast24Bean("时间", imgUrl, "温度20c"))
    list24.add(Forecast24Bean("时间2", imgUrl, "温度20c"))
    list24.add(Forecast24Bean("时间2", imgUrl, "温度20c"))
    list24.add(Forecast24Bean("时间2", imgUrl, "温度20c"))

    val listfo = ArrayList<ForecastAfterDay>()
    listfo.add(ForecastAfterDay("时间", "date时间", imgUrl, "天气状态", "天气温度状态", "空气质量"))
    listfo.add(ForecastAfterDay("时间", "date时间", imgUrl, "天气状态", "天气温度状态", "空气质量"))
    listfo.add(ForecastAfterDay("时间", "date时间", imgUrl, "天气状态", "天气温度状态", "空气质量"))
    listfo.add(ForecastAfterDay("时间", "date时间", imgUrl, "天气状态", "天气温度状态", "空气质量"))

    ComposeWeatherProjectTheme {
        val bean = LocationBean(
            "name", "20c", "温度状态",
            "湿度状态", "空气质量", "时间1111111111111111111111111111111111111", imgUrl,
            "24小时时间", list24, listfo
        )
        Greeting2(bean)
    }
}


